/**
 * Created by user on 2015/11/9.
 */

angular.module('mis.userScoreManager.controllers', [])
    .controller('userScoreCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {

        $scope.currentPage = sessionStorage.getItem('currentPage_score') ? sessionStorage.getItem('currentPage_score'):1;
        $scope.pageSize = sessionStorage.getItem('pageSize_score') ? sessionStorage.getItem('pageSize_score'):15;

        sessionStorage.removeItem('currentPage_score');
        sessionStorage.removeItem('pageSize_score');

        var params = {};
        if(sessionStorage.getItem('params_score'))params = JSON.parse(sessionStorage.getItem('params_score'));
        sessionStorage.removeItem('params_score');


        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/appUser/userScoreList/:page/:size',
            ajax: $http,
            success:function(data){
                if(data && data.data) {
                    $scope.userList = data.data.list;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;
                    $scope.pagination.totalPage = data.data.totalPage;
                }
                $scope.currentPage = $("#page_header li.am-active").text();
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                $scope.userList = null;
            },
            params: params
        });

        $scope.search = function(option, temp) {
            var target = temp.target;

            $scope.pagination.currentPage = 1;
            $scope.currentPage = 1;

            sessionStorage.removeItem('pageSize_score');
            sessionStorage.removeItem('currentPage_score');

            //var communityId = $scope.cascading.getVillageId(target);

            var body = {};
            if (option && option.communityName) body.communityName = option.communityName;
            if (option && option.userId) body._id = option.userId;
            if (option && option.phone) body.mobilePhone = option.phone;
            if (option && option.nickName) body.nickName = option.nickName;

            console.log(body)

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/appUser/userScoreList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if(data && data.data) {
                    $scope.userList = data.data.list;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;

                    $scope.pagination.totalPage = data.data.totalPage;
                }
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = body;

            }).error(function (err) {
                $scope.userList = null;
            });
        };

        $scope.scoreAddModal = function(user){

            $scope.userScore = user;

            $('#doc-modal-add').modal({target: '#doc-modal-add', width: 660, height: 430 });

        };

        $scope.showDetail = function(user){

            sessionStorage.setItem('pageSize_score',$scope.pageSize);
            sessionStorage.setItem('currentPage_score',$("#page_header li.am-active").text());
            sessionStorage.setItem('params_score',JSON.stringify(params));

            $location.path('/userScoreDetail/' + user.objId +'/'+user.nickName);
        };

        $scope.scoreAdd = function(score,rason){

           var opt ={
               userId : $scope.userScore.objId,
               scoreAddCount : Number(score),
               scoreAddRason : rason
           };
            console.log(opt)

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/appUser/addScoreManual', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if (data.result.status == 1) {
                    alert('添加成功');
                    $scope.score = 0;
                    $scope.rason = 0;
                } else {
                    alert('添加失败')
                }

                $('#doc-modal-add').modal('close')
            }).error(function (data) {
                $('#doc-modal-add').modal('close')
            });

        };

        ////级联框
        //var cascadData = {
        //    provinceUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionPrivince',
        //    cityUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCity',
        //    cityAreaUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCounty',
        //    villageUrl: 'http://' + backOfficeHost + '/rest/v1.0/mis/community/communityList',
        //    ajax: $http
        //};
        //$scope.cascading = new gObj.cascading(cascadData);
        //$scope.cascading.paint($(".city-cascading")[0],true);


    }]).controller('userScoreDetailCtrl', ['$scope','$http', '$location','$routeParams', function ($scope, $http, $location,$routeParams) {

        $scope.currentPage = 1;
        $scope.pageSize = 15;

        var userId = $routeParams.objId;
        $scope.nickName = $routeParams.name;

        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/appUser/userScoreDetail/:page/:size',
            ajax: $http,
            success:function(data){
                if(data && data.data) {
                    $scope.userList = data.data.list;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;
                    $scope.pagination.totalPage = data.data.totalPage;
                }
                $scope.currentPage = $("#page_header li.am-active").text();
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");

            },
            error: function(err){
                $scope.userList = null;
            },
            params: {userId :userId}
        });

        $scope.search = function(option, target) {

            $scope.pagination.currentPage = 1;
            $scope.currentPage = 1;

            var body = {userId :userId};

            if ($("#startDate").val()) body.startDate = $("#startDate").val() + ' 00:00:00';
            if ($("#endDate").val()) body.endDate = $("#endDate").val() + ' 23:59:59';
            if (body.startDate && body.endDate && body.startDate > body.endDate) {
                alert('开始日期不能大于结束日期');
                return;
            }

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/appUser/userScoreDetail/' + $scope.currentPage + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if(data && data.data) {
                    $scope.userList = data.data.list;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;

                    $scope.pagination.totalPage = data.data.totalPage;
                }
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = body;

            }).error(function (err) {
                $scope.userList = null;
            });
        };

        $scope.back = function () {
            $location.path('/userScoreList');
        };

    }])